package com.ruk.simplehelpers;

import com.intellij.openapi.actionSystem.DataContext;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.EditorModificationUtil;
import com.intellij.openapi.editor.SelectionModel;
import com.intellij.openapi.editor.actionSystem.EditorWriteActionHandler;

public class CamelCaseToSentenceActionHandler extends EditorWriteActionHandler {
    public void executeWriteAction(Editor editor, DataContext dataContext) {
        SelectionModel selectionModel = editor.getSelectionModel();
        if (!selectionModel.hasBlockSelection() && selectionModel.hasSelection()) {
            int selectionStart = selectionModel.getSelectionStart();
            int selectionEnd = selectionModel.getSelectionEnd();

            Document document = editor.getDocument();
            String selectedText = document.getCharsSequence().subSequence(selectionStart, selectionEnd).toString();
            EditorModificationUtil.insertStringAtCaret(editor, StringUtils.camelCaseToSentence(selectedText));
        }
    }
}
